import { getLineOptions } from './line.js'
import { getBarOptions } from './bar.js'
import { getPieOptions } from './pie.js'

function lightenColor(hex: any, percent: any) {
  const num = parseInt(hex.slice(1), 16)
  const r = Math.min(255, (num >> 16) + percent)
  const g = Math.min(255, ((num >> 8) & 0x00ff) + percent)
  const b = Math.min(255, (num & 0x0000ff) + percent)
  return `#${((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1)}`
}

function applyDarkTheme(config: any) {
  const darkColors = {
    text: '#fff',
    background: '#2c2c2c',
    gridLine: '#444',
    axisLine: '#666',
  }

  // 标题样式
  if (config.title && config.title.textStyle) {
    config.title.textStyle.color = darkColors.text
  }

  // 图例样式
  if (config.legend && config.legend.textStyle) {
    config.legend.textStyle.color = darkColors.text
  }

  // 坐标轴样式
  if (
    config.xAxis &&
    config.xAxis.axisLine &&
    config.xAxis.axisLine.lineStyle
  ) {
    config.xAxis.axisLine.lineStyle.color = darkColors.axisLine
  }
  if (config.xAxis && config.xAxis.axisLabel) {
    config.xAxis.axisLabel.color = darkColors.text
  }

  if (
    config.yAxis &&
    config.yAxis.axisLine &&
    config.yAxis.axisLine.lineStyle
  ) {
    config.yAxis.axisLine.lineStyle.color = darkColors.axisLine
  }
  if (config.yAxis && config.yAxis.axisLabel) {
    config.yAxis.axisLabel.color = darkColors.text
  }

  // 网格线
  if (
    config.yAxis &&
    config.yAxis.splitLine &&
    config.yAxis.splitLine.lineStyle
  ) {
    config.yAxis.splitLine.lineStyle.color = darkColors.gridLine
  }

  // 背景色
  config.backgroundColor = darkColors.background
}

// 折线图生成函数
function generateLineChart(params: any) {
  const template = getLineOptions()
  let configString = JSON.stringify(template)
  if (params.title) {
    configString = configString.replace(/{title}/g, params.title)
  }
  if (params.seriesName) {
    configString = configString.replace(/{seriesName}/g, params.seriesName)
  }
  // 解析回对象
  const config = JSON.parse(configString)

  // 设置数据
  config.xAxis.data = params.xAxisData
  config.series[0].data = params.yAxisData

  // 设置坐标轴名称
  if (params.xAxisName) {
    config.xAxis.name = params.xAxisName
    config.xAxis.nameTextStyle = {
      color: '#666',
      fontSize: 12,
    }
  }

  if (params.yAxisName) {
    config.yAxis.name = params.yAxisName
    config.yAxis.nameTextStyle = {
      color: '#666',
      fontSize: 12,
    }
  }

  if (params.theme === 'dark') {
    applyDarkTheme(config)
  }

  return config
}

// 柱状图生成函数
function generateBarChart(params: any) {
  const template = getBarOptions()
  let configString = JSON.stringify(template)
  if (params.title) {
    configString = configString.replace(/{title}/g, params.title)
  }
  if (params.seriesName) {
    configString = configString.replace(/{seriesName}/g, params.seriesName)
  }

  const config = JSON.parse(configString)

  // 设置数据
  config.xAxis.data = params.xAxisData
  config.series[0].data = params.yAxisData

  // 设置坐标轴名称
  if (params.xAxisName) {
    config.xAxis.name = params.xAxisName
    config.xAxis.nameTextStyle = {
      color: '#666',
      fontSize: 12,
    }
  }

  if (params.yAxisName) {
    config.yAxis.name = params.yAxisName
    config.yAxis.nameTextStyle = {
      color: '#666',
      fontSize: 12,
    }
  }

  // 应用自定义颜色
  const barColor = params.barColor || '#5470c6'
  config.series[0].itemStyle.color = barColor
  config.series[0].emphasis.itemStyle.color = lightenColor(barColor, 20)

  // 应用其他自定义选项
  if (params.barWidth) {
    config.series[0].barWidth = params.barWidth
  }

  if (params.showLabel === false) {
    config.series[0].label.show = false
  }

  if (params.borderRadius !== undefined) {
    config.series[0].itemStyle.borderRadius = [
      params.borderRadius,
      params.borderRadius,
      0,
      0,
    ]
  }

  if (params.theme === 'dark') {
    applyDarkTheme(config)
  }

  return config
}

// 扇形图生成函数
function generatePieChart(params: any) {
  const template = getPieOptions()
  let configString = JSON.stringify(template)
  if (params.title) {
    configString = configString.replace(/{title}/g, params.title)
  }
  if (params.seriesName) {
    configString = configString.replace(/{seriesName}/g, params.seriesName)
  }

  const config = JSON.parse(configString)

  // 设置数据
  if (
    params.xAxisData &&
    params.yAxisData &&
    params.xAxisData.length === params.yAxisData.length
  ) {
    const pieData = params.xAxisData.map((name: any, index: number) => ({
      name: name,
      value: params.yAxisData[index],
    }))

    config.series[0].data = pieData
    config.legend.data = params.xAxisData
  } else {
    throw new Error(
      'For pie charts, xAxisData and yAxisData must be arrays of the same length'
    )
  }

  // 自定义分类颜色
  if (params.pieColors) {
    config.series[0].itemStyle.color = (params: any) => {
      const colorList = params.pieColors || [
        '#5470c6',
        '#91cc75',
        '#fac858',
        '#ee6666',
        '#73c0de',
        '#3ba272',
        '#fc8452',
        '#9a60b4',
        '#ea7ccc',
        '#d7504b',
      ]
      return colorList[params.dataIndex % colorList.length]
    }
  }

  // 半径设置
  if (params.pieRadius) {
    if (Array.isArray(params.pieRadius) && params.pieRadius.length === 2) {
      config.series[0].radius = params.pieRadius
    } else if (
      typeof params.pieRadius === 'string' ||
      typeof params.pieRadius === 'number'
    ) {
      config.series[0].radius = params.pieRadius
    }
  }

  if (params.showLabel === false) {
    config.series[0].label.show = false
    config.series[0].labelLine.show = false
  }

  if (params.theme === 'dark') {
    applyDarkTheme(config)
  }

  return config
}

export { generateLineChart, generateBarChart, generatePieChart }

export * from './line.js'
export * from './bar.js'
export * from './pie.js'
